require('es6-promise').polyfill();
require('isomorphic-fetch');
const config = {
	serverUrl : "http://211.159.184.127:3000",
	clientUrl : "http://127.0.0.1:9000"
};
export default function request(method, url, body) {
	const baseUrl = config.serverUrl;
	url = baseUrl + url;
	method = method.toUpperCase();
	let contentType = 'application/json';
	if (method === 'GET') {
		body = undefined;
	}
	else if (method === 'FORM-POST') {
		let str = '';
		contentType = 'application/json';
		return fetch(url, {
			method : "POST",
			body : JSON.stringify(body),
			'Content-Type': contentType,
		}).then((res) => {
			return res.json();
		});
	}
	else{
		return fetch(url, {
			method : "POST",
			headers: {
				"authorization" : "Token " + localStorage.getItem("fleaToken")
			},
			body : JSON.stringify(body),
			'Content-Type': contentType,
		}).then((res) => {
			return res.json();
		});
	}
}

export const get = (url) => request('GET', url,null);
export const post = (url, body) => request('POST', url, body);
export const formPost = (url, body) => request('FORM-POST', url, body);